package edu.kpi.pzks2.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;
import edu.kpi.pzks2.graph.Edge;
import edu.kpi.pzks2.graph.Node;
import edu.kpi.pzks2.ui.simulation.SimulationFrame;
import edu.kpi.pzks2.ui.system.SystemFrame;
import edu.kpi.pzks2.ui.task.TaskFrame;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.Graph;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
    public static final String NOT_LOADED = "Not loaded";
    public static final String LOADED = "Loaded";
    public static final String TITLE = "PZKS2 labs";

    private final LoadLabel taskLabel;
    private final LoadLabel systemLabel;

    private TaskFrame taskEditorFrame;
    private SystemFrame systemEditorFrame;
    private SimulationFrame simulationFrame;

    private static JTextPane logPane;
    
    private static String helpString = 
            "<html>"+
                    "<h3>Help<h3>" +
                    "<p>This is task simulator.</p>" +
                    "<p>For start simulation you should load or create task and system graph " +
                    "(use correspond buttons for this purposes).</p>" +
            "</html>";

    public MainFrame() {
        getContentPane().setLayout(new MigLayout("", "[grow][][][]", "[][][grow]"));                
        setTitle(TITLE);

        MainMenuButton taskButton = new MainMenuButton("Task", "/task.png");
        taskButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (taskEditorFrame == null) {
                    taskEditorFrame = new TaskFrame(taskLabel);
                }
                taskEditorFrame.setVisible(true);
            }
        });
        getContentPane().add(taskButton, "cell 0 0,growx");

        MainMenuButton systemButton = new MainMenuButton("System", "/system.png");
        systemButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (systemEditorFrame == null) {
                    systemEditorFrame = new SystemFrame(systemLabel);
                }
                
                systemEditorFrame.setVisible(true);
            }
        });
        getContentPane().add(systemButton, "cell 1 0,growx");

        MainMenuButton simulateButton = new MainMenuButton("Simulate", "/simulation.png");
        getContentPane().add(simulateButton, "cell 2 0");

        simulationFrame = new SimulationFrame(this);
        simulateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (systemLabel.loaded && taskLabel.loaded) {
                    simulationFrame.setVisible(true);
                }
            }
        });
        
        MainMenuButton helpButton = new MainMenuButton("Help", "/help.png");
        getContentPane().add(helpButton, "cell 3 0");

        helpButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(MainFrame.this, helpString);
            }
        });

        taskLabel = new LoadLabel();
        getContentPane().add(taskLabel, "cell 0 1,alignx center");

        systemLabel = new LoadLabel();
        getContentPane().add(systemLabel, "cell 1 1,alignx center");
        
        logPane = new JTextPane();
        logPane.setText("Log pane\n");
        getContentPane().add(new JScrollPane(logPane), "cell 0 2 4 1,grow");

        setSize(450, 320);
        //setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static class MainMenuButton extends JButton {
        public MainMenuButton(String label, String iconPath) {
            super(label, createImageIcon(iconPath));
            setVerticalTextPosition(AbstractButton.BOTTOM);
            setHorizontalTextPosition(AbstractButton.CENTER);
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    public static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = MainFrame.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    public static void showLogMessage(String message) {
        if (logPane != null) {
            logPane.setText(logPane.getText() + message + "\n");
        }
    }
    
    public void setTaskLoadLabel(boolean flag) {
        taskLabel.setLoaded(flag);
    }
    
    public void setSystemLoadLabel(boolean flag) {
        taskLabel.setLoaded(flag);
    }
    
    public static class LoadLabel extends JLabel {
        private boolean loaded;

        public LoadLabel() {
            setLoaded(false);
        }

        public void setLoaded(boolean loaded) {
            this.loaded = loaded;

            if (loaded) {
                setText(LOADED);
                setForeground(Color.GREEN);
            } else {
                setText(NOT_LOADED);
                setForeground(Color.RED);
            }
        }
    }
    
    public DirectedGraph<Node, Edge> getTaskGraph() {
        return taskEditorFrame.getTaskGraph();
    }
    
    public Graph<Node, Edge> getSystemGraph() {
        return systemEditorFrame.getSystemGraph();
    }
}
